/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Canvas.java
 *
 * Created on 28-ago-2010, 15:24:20
 */

package umis2.vista;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.util.LinkedList;
import java.util.List;
import umis2.controladorcentral.Forma;
import umis2.manejadordepersonajes.Teclado;

/**
 *
 * @author djphilomath
 */
public class Canvas extends javax.swing.JPanel implements KeyListener{

    List<Forma> formas;
    public int x;
    public int anterior;
    public final Object formasCandado = new Object();
   // Teclado teclado = Teclado.getInstance();
    /** Creates new form Canvas */
    public Canvas() {
        initComponents();
        formas = new LinkedList<Forma>();
        x=0;
        anterior=5;


    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        //g2.setClip(x, 0, g2.getClip().getBounds().width, g2.getClip().getBounds().height);
        this.setBounds(10-x, getY(), getWidth()+x, getHeight());
        anterior=anterior+x;

       synchronized(formasCandado){
            for (Forma f : formas) {
                int x = f.getPosicion().x;
                int y = f.getPosicion().y;
                g2.drawImage(f.getImage(), new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_NEAREST_NEIGHBOR), x, y);
            }
            for (umis2.nivel.Plataforma p : Vista.getNivel().getPlataformas()){
                //g2.draw(p.getRectangulo());
            }
            for (umis2.nivel.Personaje p : Vista.getNivel().getPersonajes()){
                //g2.draw(p.getRectangulo());
            }
        }

 /*
        Forma f = null;
        for(int i = 0; i < formas.size();i++){
            f = formas.get(i);
            g2.drawImage(f.getImage(), new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_NEAREST_NEIGHBOR), f.getPosicion().x, f.getPosicion().y);

        }
*/
        
        repaint();
    }

    public void setFormas(List<Forma> f) {
        //synchronized(formasCandado){
        this.formas = f;
        //}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("not supported yet");
    }

    public void keyPressed(KeyEvent e) {
        int action = e.getKeyCode();
        Teclado teclado = Teclado.getInstance();
        switch(action){
            case KeyEvent.VK_RIGHT:
                teclado.moverDerecha();
                System.out.println("Hola");
                break;
            case KeyEvent.VK_LEFT:
                teclado.moverIzquierda();
                break;
            case KeyEvent.VK_UP:
                teclado.saltar();
                break;
        }
    }

    public void keyReleased(KeyEvent e) {
        int action = e.getKeyCode();

        switch(action){
            case KeyEvent.VK_RIGHT:

                break;
            case KeyEvent.VK_LEFT:

                break;
            case KeyEvent.VK_SPACE:
                
                break;
        }
    }

    public void mover(int x){
        this.x=x;
    }
/*
    public void run() {
        while (true) {
            Vista.transformador.actualizarVista();

            
        }
    }
*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
